package com.test.web3j.util;

import java.math.BigInteger;

import org.web3j.crypto.Credentials;
import org.web3j.protocol.core.methods.response.TransactionReceipt;
import org.web3j.tx.Contract;

import com.test.web3j.Web3JClient;
import com.test.web3j.contract.Aoabank;

/**
 * AoaBank工具类
 */
public class AoaBankUtil {
	
	/**
	 * 部署智能合约
	 * @param credentials
	 * @return 智能合约对象
	 * @throws Exception
	 */
	public static Aoabank deployContract(String privateKey) {
		Credentials credentials = Credentials.create(privateKey);
		Aoabank aoabank = null;
		try {
			aoabank = Aoabank.deploy(
					Web3JClient.getWeb3j(), 
					credentials, 
					Contract.GAS_PRICE, 
					Contract.GAS_LIMIT).send();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return aoabank;
	}
	
	/**
	 * 通过智能合约地址，加载智能合约
	 * @param contractAddress
	 * @param privateKey
	 * @return
	 */
	public static Aoabank loadContract(String contractAddress, String privateKey) {
		Credentials credentials = Credentials.create(privateKey);
		Aoabank aoabank = Aoabank.load(
				contractAddress, 
				Web3JClient.getWeb3j(), 
				credentials, 
				Contract.GAS_PRICE, 
				Contract.GAS_LIMIT);
		return aoabank;
	}
	
	/**
	 * 查询余额
	 * @throws Exception 
	 */
	public static BigInteger balanceOf(Aoabank aoabank, String address) {
		BigInteger balance = null;
		try {
			balance = aoabank.balanceOf(address).send();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return balance;
	}
	
	/**
	 * 转账
	 * @param aoabank
	 * @param ownAddress
	 * @param toAddress
	 * @param transferAmount
	 * @return 交易hash
	 */
	public static String transferFrom(Aoabank aoabank, String ownAddress, String toAddress, BigInteger transferAmount) {
		TransactionReceipt transactionReceipt = null;
		try {
			transactionReceipt = aoabank.transfer(toAddress, transferAmount).send();
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(transactionReceipt!=null) {
			return transactionReceipt.getTransactionHash();
		}
		return null;
	}
	
	/**
	 * main
	 */
	public static void main(String[] args) {
		// 发布合约
		String privateKey = "0xc69d6fe0434e1740009f0f2ef48d27112943628a6f81cb53f1a1d64b70ef2e9d";  // 任意address的credentials
//		Aoabank aoabank = deployContract(privateKey);
//		System.out.println("部署智能合约成功，合约地址：" + aoabank.getContractAddress());
		// 0xdf5d227dd53cd4e4512db1510c332c7384405cc5
		
		// 加载合约
		String contractAddress = "0x04e74bd5fd855b0ab3f0a00ddc23b543f26e22bd"; // 智能合约地址（合约发布之后不可以修改）
		Aoabank aoabank = loadContract(contractAddress, privateKey);
				
		// 查询余额
		String address = "0xeaf88024c5f5b473c732cd41a1931445281ceb41";
		BigInteger balance = balanceOf(aoabank, address);
		System.out.println("balance:" + balance);
		
		// 转账
		String fromAddress = "0xeaf88024c5f5b473c732cd41a1931445281ceb41";
		String toAddress = "0xd5169b925874898904b1006f4c467bae45e8c46e";
		BigInteger amount = BigInteger.valueOf(100000l);
		String txId = transferFrom(aoabank, fromAddress, toAddress, amount);
		System.out.println("txId: " + txId);
		// 0xe35772ad31ed2280bd9c24b25ddbfa75cb591a3ae3c55fc71e29798b98bc2a0e
	}
	
}
